﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MonoDevelop.Ide.Gui;
using MonoDevelop.Components;
using MonoDevelop.Components.Docking;
using MonoDevelop.Components.Commands;
using MonoDevelop.Core;
using MonoDevelop.Ide;

using Gtk;
using Gdk;


namespace Coq.MonoDevelop
{
    public class CoqProofPad : IPadContent
    {
        TextView view = new TextView();

        public static CoqProofPad CurrentProof
        {
            get
            {
                return (CoqProofPad)CurrentPad.Content;
            }
        }

        public static Pad CurrentPad
        {
            get
            {
                Pad ret = null;
                try
                {
                    ret = IdeApp.Workbench.GetPad<CoqProofPad>();
                }
                catch
                { }
                if (ret != null)
                    return ret;
                else
                    ret = IdeApp.Workbench.AddPad(new CoqProofPad(), "Coq.MonoDevelop.CoqProofPad", "Coq Proof", "Right Top", IconId.Null);
                return ret;
            }
        }



        #region Interface
        public Widget Control { get { return view; } }

        public void Dispose()
        {
        }

        public void Initialize(IPadWindow window)
        {
            view.WrapMode = WrapMode.WordChar;
            view.Editable = false;
            view.ShowAll();
            //view.ConsoleInput += (s, e) => { Session.SendCommand(e.Text); };

            //var toolbar = window.GetToolbar(Gtk.PositionType.Right);
            //var buttonClear = new DockToolButton("gtk-clear");
            //buttonClear.TooltipText = GettextCatalog.GetString("Clear");
            //buttonClear.Clicked += (s, e) => { view.Clear(); };

            //toolbar.Add(buttonClear);
            //toolbar.ShowAll();
        }

        public void SetProof(string str)
        {
            view.Buffer.Text = str;
        }

        public void RedrawContent()
        {
        }

        #endregion
    }


    public class CoqOutputPad : IPadContent
    {
        TextView view = new TextView() ;
        bool prompting = false;
        bool enabled = false;
        string lastCommand = "";
        string currentPath = "";

        public static CoqOutputPad CurrentOutput
        {
            get
            {
                return (CoqOutputPad)CurrentPad.Content;
            }
        }

        public static Pad CurrentPad
        { 
            get {
                    Pad ret = null;
                    try
                    {
                        ret = IdeApp.Workbench.GetPad<CoqOutputPad>();
                    }
                    catch 
                    { }
                    if (ret != null)
                        return ret;
                    else
                        ret = IdeApp.Workbench.AddPad(new CoqOutputPad(), "Coq.MonoDevelop.CoqOutputPad", "Coq Output", "Right Bottom", IconId.Null);
                    return ret;
                }  
        }

        #region Interface
        public Widget Control { get {  return view; } }

        void TextEditor_CursorPositionChanged(object sender, EventArgs e)
        {
            //Errors and Outputs
            var line = IdeApp.Workbench.ActiveDocument.TextEditor.CursorLine;
            var column = IdeApp.Workbench.ActiveDocument.TextEditor.CursorColumn;
            var pos = new Microsoft.FSharp.Text.Lexing.Position(null,line,0,column);
            var m = CoqProcessAgent.Service.RetrieveMessage(pos);
            var p = CoqProcessAgent.Service.ProofMessage(pos);
            if (m == null || m.Value == null)
                view.Buffer.Text = "";
            else if (m.Value is Message.Error)
            {
                var er = m.Value as Message.Error;
                view.Buffer.Text = er.Item1;
            }
            else if (m.Value is Message.Output)
            {
                var o = m.Value as Message.Output;
                view.Buffer.Text = o.Item;
            }
            else
                view.Buffer.Text = "";
            //Proofs - displayed until proof completed 
            CoqProofPad.CurrentProof.SetProof(p);
            

        }

        void Workbench_ActiveDocumentChanged(object sender, EventArgs e)
        {
            //if (!enabled)
                if (IdeApp.Workbench.ActiveDocument != null)
                {
                    var o = IdeApp.Workbench.ActiveDocument.GetType();
                    try
                    {
                        if (IdeApp.Workbench.ActiveDocument != null)
                            IdeApp.Workbench.ActiveDocument.UpdateParseDocument();
                    }
                    catch { }
                    try
                    {
                        IdeApp.Workbench.ActiveDocument.TextEditor.CursorPositionChanged += new EventHandler(TextEditor_CursorPositionChanged);
                        enabled = true;
                    }
                    catch
                    {
                        
                    }
                }

        }

        public void Dispose()
        {
        }

        public void Initialize(IPadWindow window)
        {
            IdeApp.Workbench.ActiveDocumentChanged += new EventHandler(Workbench_ActiveDocumentChanged);
            
            view.WrapMode = WrapMode.WordChar;
            view.Editable = false;
            view.ShowAll();
        }

        

        public void RedrawContent()
        {
        }

        #endregion 
    }
}
